<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

    <script>
        //过滤空白字符串
       // /^\s*|\s*$/g
       function abc() {
        var oldstr = document.getElementById('old');
        var newstr = document.getElementById('new');
        var reg=/^\s*|\s*$/g;
        newstr.value=oldstr.value.replace(reg,'')
       }
    </script>
    <br>
        过滤空格前的字符串:<input type="text" id="old">
      <button onclick="abc()">过滤空格</button>
      <br>过滤空格后的字符串:<input type="text" id="new"></br>
</body>
</html>
